﻿using Commons.ReturnData;
using IdentityServer.Domain.Entities;
using IdentityServer.Domain.IRepositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IdentityServer.Infrastructure.Repositories
{
    public class WeChatRepository : IWeChatRepository
    {
        private readonly IdDbContext context;

        public WeChatRepository(IdDbContext context)
        {
            this.context = context;
        }

        public async Task<MessageResult> CreateWechatUserAsync(WeChatUser weChatUser)
        {
            await context.WeChatUsers.AddAsync(weChatUser);
            try
            {
                await context.SaveChangesAsync();
                return new MessageResult("创建成功！");
            }
            catch (Exception)
            {

                return new MessageResult(405, "创建失败！", false);
            }
        }

        public async Task<WeChatUser?> FindByOpenIdAsync(string openId)
        {
            return await context.WeChatUsers.FirstOrDefaultAsync(x=> x.OpenId == openId);
        }
    }
}
